home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / ediff-diff.el < prev    next >
Lisp/Scheme  |  1996-07-02  |  44KB  |  1,196 lines

  1. ;;; ediff-diff.el --- diff-related utilities
  2.  
  3. ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  21. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. ;; Boston, MA 02111-1307, USA.
  23.  
  24. ;;; Code:
  25.  
  26. (require 'ediff-init)
  27.  
  28.  
  29. (defvar ediff-shell
  30.   (cond ((eq system-type 'emx) "cmd") ; OS/2
  31.     ((memq system-type '(ms-dos windows-nt windows-95))
  32.      shell-file-name) ; no standard name on MS-DOS
  33.     ((memq system-type '(vax-vms axp-vms)) "*dcl*") ; VMS
  34.     (t  "sh")) ; UNIX
  35.   "*The shell used to run diff and patch.  If user's .profile or
  36. .cshrc files are set up correctly, any shell will do.  However, some people
  37. set $prompt or other things incorrectly, which leads to undesirable output
  38. messages.  These may cause Ediff to fail.  In such a case, set ediff-shell
  39. to a shell that you are not using or, better, fix your shell's startup file.")
  40.  
  41.  
  42. (defvar ediff-diff-program "diff"
  43.   "*Program to use for generating the differential of the two files.")
  44. (defvar ediff-diff-options ""  
  45.   "*Options to pass to `ediff-diff-program'. 
  46. If diff\(1\) is used as `ediff-diff-program', then the most useful options are
  47. `-w', to ignore space, and `-i', to ignore case of letters.
  48. At present, the option `-c' is ignored, since Ediff doesn't understand this
  49. type of output.")
  50.  
  51. (defvar ediff-custom-diff-program ediff-diff-program
  52.   "*Program to use for generating custom diff output for saving it in a file.
  53. This output is not used by Ediff internally.")
  54. (defvar ediff-custom-diff-options "-c"
  55.   "*Options to pass to `ediff-custom-diff-program'.")
  56.  
  57. ;;; Support for diff3
  58.  
  59. (defvar ediff-match-diff3-line "^====\\(.?\\)$"
  60.   "Pattern to match lines produced by diff3 that describe differences.")
  61. (defvar ediff-diff3-program "diff3"
  62.   "*Program to be used for three-way comparison.
  63. Must produce output compatible with Unix's diff3 program.")
  64. (defvar ediff-diff3-options ""  
  65.   "*Options to pass to `ediff-diff3-program'.")
  66. (defvar ediff-diff3-ok-lines-regexp
  67.   "^\\([1-3]:\\|====\\|  \\|.*Warning *:\\|.*No newline\\|.*missing newline\\|^\C-m$\\)"
  68.   "*Regexp that matches normal output lines from `ediff-diff3-program'.
  69. Lines that do not match are assumed to be error messages.")
  70.  
  71. ;; keeps the status of the current diff in 3-way jobs.
  72. ;; the status can be =diff(A), =diff(B), or =diff(A+B)
  73. (ediff-defvar-local ediff-diff-status "" "")
  74.  
  75.   
  76. ;;; Fine differences 
  77.  
  78. (ediff-defvar-local ediff-auto-refine (if (ediff-has-face-support-p) 'on 'nix)
  79.   "If `on', Ediff auto-highlights fine diffs for the current diff region.
  80. If `off', auto-highlighting is not used. If `nix', no fine diffs are shown
  81. at all, unless the user force-refines the region by hitting `*'.
  82.  
  83. This variable can be set either in .emacs or toggled interactively.
  84. Use `setq-default' if setting it in .emacs")
  85.  
  86. (ediff-defvar-local ediff-ignore-similar-regions nil
  87.   "*If t, skip over difference regions that differ only in the white space and line breaks.
  88. This variable can be set either in .emacs or toggled interactively.
  89. Use `setq-default' if setting it in .emacs")
  90.  
  91. (ediff-defvar-local ediff-auto-refine-limit 1400
  92.   "*Auto-refine only the regions of this size \(in bytes\) or less.")
  93.   
  94. ;;; General
  95.  
  96. (defvar ediff-diff-ok-lines-regexp  
  97.   "^\\([0-9,]+[acd][0-9,]+$\\|[<>] \\|---\\|.*Warning *:\\|.*No +newline\\|.*missing +newline\\|^\C-m$\\)"
  98.   "Regexp that matches normal output lines from `ediff-diff-program'.
  99. This is mostly lifted from Emerge, except that Ediff also considers
  100. warnings and `Missing newline'-type messages to be normal output.
  101. Lines that do not match are assumed to be error messages.")
  102.  
  103. (defvar ediff-match-diff-line (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
  104.                 (concat "^" x "\\([acd]\\)" x "$"))
  105.   "Pattern to match lines produced by diff that describe differences.")
  106.  
  107. (ediff-defvar-local ediff-setup-diff-regions-function nil
  108.   "value is a function symbol depending on the kind of job is to be done.
  109. For 2-way jobs and for ediff-merge, it should be `ediff-setup-diff-regions'.
  110. For jobs requiring diff3, it should be `ediff-setup-diff-regions3'.
  111.  
  112. The function should take three mandatory arguments, file-A, file-B, and
  113. file-C. It may ignore file C for diff2 jobs. It should also take
  114. one optional arguments, diff-number to refine.")
  115.  
  116.   
  117. ;;; Functions
  118.  
  119. ;; Generate the difference vector and overlays for the two files
  120. ;; With optional arg REG-TO-REFINE, refine this region.
  121. ;; File-C argument is not used here. It is there just because
  122. ;; ediff-setup-diff-regions is called via a funcall to
  123. ;; ediff-setup-diff-regions-function, which can also have the value
  124. ;; ediff-setup-diff-regions3, which takes 4 arguments.
  125. (defun ediff-setup-diff-regions (file-A file-B file-C)
  126. ;;;  ;; Force all minibuffers to display ediff's messages.
  127. ;;;  ;; When xemacs implements minibufferless frames, this won't be necessary
  128. ;;;  (if ediff-xemacs-p (setq synchronize-minibuffers t))
  129.                           
  130.   ;; create, if it doesn't exist
  131.   (or (ediff-buffer-live-p ediff-diff-buffer)
  132.       (setq ediff-diff-buffer
  133.         (get-buffer-create (ediff-unique-buffer-name "*ediff-diff" "*"))))
  134.   (ediff-make-diff2-buffer ediff-diff-buffer file-A file-B)
  135.   (ediff-prepare-error-list ediff-diff-ok-lines-regexp ediff-diff-buffer)
  136.   (ediff-convert-diffs-to-overlays
  137.    (ediff-extract-diffs
  138.     ediff-diff-buffer ediff-word-mode ediff-narrow-bounds)))
  139.  
  140. ;; Run the diff program on FILE1 and FILE2 and put the output in DIFF-BUFFER
  141. ;; Return the size of DIFF-BUFFER
  142. (defun ediff-make-diff2-buffer (diff-buffer file1 file2)
  143.   (cond ((< (ediff-file-size file1) 0)
  144.      (message "Can't diff remote files: %s"
  145.           (ediff-abbreviate-file-name file1))
  146.      (sit-for 2)
  147.      ;; 1 is an error exit code
  148.      1)
  149.     ((< (ediff-file-size file2) 0)
  150.      (message "Can't diff remote file: %s"
  151.           (ediff-abbreviate-file-name file2))
  152.      (sit-for 2)
  153.      (message "")
  154.      ;; 1 is an error exit code
  155.      1)
  156.     (t (message "Computing differences between %s and %s ..."
  157.             (file-name-nondirectory file1)
  158.             (file-name-nondirectory file2))
  159.        ;; this erases the diff buffer automatically
  160.        (ediff-exec-process ediff-diff-program
  161.                    diff-buffer
  162.                    'synchronize
  163.                    ediff-diff-options file1 file2)
  164.        ;;(message "Computing differences ... done")
  165.        (message "")
  166.        (ediff-eval-in-buffer diff-buffer
  167.          (buffer-size)))))
  168.   
  169.  
  170.      
  171. ;; If file-A/B/C is nil, do 2-way comparison with the non-nil buffers
  172. ;; This function works for diff3 and diff2 jobs
  173. (defun ediff-setup-fine-diff-regions (file-A file-B file-C reg-num)
  174.   (or (ediff-buffer-live-p ediff-fine-diff-buffer)
  175.       (setq ediff-fine-diff-buffer
  176.         (get-buffer-create
  177.          (ediff-unique-buffer-name "*ediff-fine-diff" "*"))))
  178.   
  179.   (let (diff3-job diff-program diff-options ok-regexp diff-list)
  180.     (setq diff3-job ediff-3way-job
  181.       diff-program (if diff3-job ediff-diff3-program ediff-diff-program)
  182.       diff-options (if diff3-job ediff-diff3-options ediff-diff-options)
  183.       ok-regexp (if diff3-job
  184.             ediff-diff3-ok-lines-regexp
  185.             ediff-diff-ok-lines-regexp))
  186.     
  187.     (ediff-message-if-verbose "Refining difference region %d ..." (1+ reg-num))
  188.     (ediff-exec-process diff-program ediff-fine-diff-buffer 'synchronize
  189.             diff-options
  190.             ;; The shuffle below is because we can compare 3-way
  191.             ;; or in several 2-way fashions, like fA fC, fA fB,
  192.             ;; or fB fC.
  193.             (if file-A file-A file-B)
  194.             (if file-B file-B fi